package com.yanqu.road.dao.impl.activity.biaoju;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuCityData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuCityImpl extends TempDataObjectDao<CrossBiaoJuCityData> {

    public static String getCrossBiaoJuCityDataSql = "select * from `t_c_biaoju_city` where `activityId`=? and `unionGroup`=?";
    public Map<String, Map<Integer, CrossBiaoJuCityData>> getCrossBiaoJuCityData(int activityId,int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, CrossBiaoJuCityData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossBiaoJuCityDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossBiaoJuCityData data = parse(rs);
                if (dataMap.get(data.getUnionId()) == null) {
                    dataMap.put(data.getUnionId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUnionId()).put(data.getCityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossBiaoJuCityDataSql = "update `t_c_biaoju_city` set score=?,roundScore=?,scoreChangeTime=?,rank=?,lastRank=?,lastRankScore=?,historyRankMap=?,memberAddScoreMap=? where activityId=? and cityId=? and unionId=? and unionGroup=?";
    @Override
    protected boolean updateData(CrossBiaoJuCityData crossBiaoJuCityData) {
        crossBiaoJuCityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getRoundScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getScoreChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getLastRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getLastRankScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuCityData.getHistoryRankMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuCityData.getMemberAddScoreMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getCityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuCityData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossBiaoJuCityDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossBiaoJuCityDataSql = "insert into `t_c_biaoju_city`(activityId,cityId,unionId,unionGroup,score,roundScore,scoreChangeTime,rank,lastRank,lastRankScore,historyRankMap,memberAddScoreMap) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossBiaoJuCityData crossBiaoJuCityData) {
        crossBiaoJuCityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getCityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuCityData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getRoundScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getScoreChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuCityData.getLastRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuCityData.getLastRankScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuCityData.getHistoryRankMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuCityData.getMemberAddScoreMap())));
        if (getDb().executeNoneQuery(insertCrossBiaoJuCityDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossBiaoJuCityData parseData(ResultSet rs) throws SQLException {
        CrossBiaoJuCityData crossBiaoJuCityData = new CrossBiaoJuCityData();
        crossBiaoJuCityData.setActivityId(rs.getInt("activityId"));
        crossBiaoJuCityData.setCityId(rs.getInt("cityId"));
        crossBiaoJuCityData.setUnionId(rs.getString("unionId"));
        crossBiaoJuCityData.setUnionGroup(rs.getInt("unionGroup"));
        crossBiaoJuCityData.setScore(rs.getLong("score"));
        crossBiaoJuCityData.setRoundScore(rs.getLong("roundScore"));
        crossBiaoJuCityData.setScoreChangeTime(rs.getLong("scoreChangeTime"));
        crossBiaoJuCityData.setRank(rs.getInt("rank"));
        crossBiaoJuCityData.setLastRank(rs.getInt("lastRank"));
        crossBiaoJuCityData.setLastRankScore(rs.getLong("lastRankScore"));
        crossBiaoJuCityData.setHistoryRankMap(JSON.parseObject(rs.getString("historyRankMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.List<java.lang.Long>>>() {}));
        crossBiaoJuCityData.setMemberAddScoreMap(JSON.parseObject(rs.getString("memberAddScoreMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.biaoju.BiaoJuCityUserBuildInfo>>>() {}));

        return crossBiaoJuCityData;
    }
}
